<template>
    <el-dialog :title="isEdit?'编辑作业班组':'新增作业班组'" :visible.sync="dialogForm" width="720px">
        <el-form :inline="true" ref="form" :model="form" :rules="rules" class="add-form" label-width="110px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="作业班组编码" prop="resourceCode">
                        <el-input v-model="form.resourceCode" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="作业班组名称" prop="resourceName">
                        <el-input v-model="form.resourceName" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="所属单位" prop="orgId">
                        <auto-search-org v-model="form.orgId"
                                         @select="v=>{form.orgId=v;form.deptId='';form.deptName=''}"
                                         @selectName="v=>form.orgName=v"
                                         :disabled="true"
                                         :name="form.orgName"></auto-search-org>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="所属部门" prop="deptId">
                        <search-dept v-model="form.deptId"
                                          @select="v=>form.deptId=v"
                                          @selectName="v=>form.deptName=v"
                                          :orgId="form.orgId"
                                          :disabled="!form.orgId"
                                          :name="form.deptName"></search-dept>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="备注" prop="resourceDescribe">
                        <el-input type="textarea" :rows="3" style="width: 560px" placeholder="请输入内容"
                                  :maxLength="$maxLength"
                                  v-model="form.resourceDescribe"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogForm = false">取 消</el-button>
            <el-button type="primary" @click="handleSubmit" :loading="$store.getters.loadingBtn">保 存</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import { addWorkTeam, updateWorkTeam } from "@/api/baseResource/baseResource";
    import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
    import searchDept from '@/components/autocomplete/autoSearchDept'
    import { maxLength, sortLength } from "@/utils/rules";

    export default {
        name: "roleForm",
        components: { autoSearchOrg,searchDept },
        data() {
            return {
                form: this.clearForm(),
                rules: {
                    resourceCode: [{ required: true, message:'请输入作业班组编码', trigger: 'blur' },sortLength],
                    resourceName: [{ required: true, message:'请输入作业班组名称', trigger: 'blur' },sortLength],
                    orgId: [{ required: true, message:'请选择单位', trigger: 'change' }],
                    deptId: [{ required: true, message:'请选择部门', trigger: 'change' }],
                    resourceDescribe: [maxLength]
                },
                dialogForm: false,
                isEdit: false,
            }
        },
        methods: {
            clearForm() {
                return {
                    resourceName: '',
                    resourceCode: '',
                    resourceDescribe: '',
                    deptName:'',
                    deptId:'',
                    orgName:this.$store.state.user.currentUser.org&&this.$store.state.user.currentUser.org.name,
                    orgId:this.$store.state.user.currentUser.org&&this.$store.state.user.currentUser.org.id
                }
            },
            // 保存
            handleSubmit() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.isEdit ? this.handleToEdit() : this.handleToAdd()
                    } else {
                        this.$message({
                            message: '请检查表单',
                            type: 'error'
                        })
                    }
                })
            },
            // 新增
            handleToAdd() {
                addWorkTeam(this.form).then(res => {
                    this.$message({ message: '新增成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
            // 编辑
            handleToEdit() {
                updateWorkTeam(this.form.id, this.form).then(res => {
                    this.$message({ message: '编辑成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
        },
        watch: {
            dialogForm(bool) {
                if (!bool) {
                    this.$refs.form.resetFields()
                    this.isEdit = false
                    this.form = this.clearForm()
                    this.$emit('clear')
                }
            },
        },
    }
</script>

<style scoped>

</style>
